package com.hui.auth.web;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.hui.auth.dto.JsonResult;
import com.hui.auth.entity.AuthMenu;
import com.hui.auth.entity.AuthPermission;
import com.hui.auth.exception.AuthError;
import com.hui.auth.service.AuthPermissionService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import static org.apache.ibatis.ognl.DynamicSubscript.mid;

/**
 * 角色控制
 * Created by LiaoYunHui on 2017/11/5/005.
 */
@Api(tags = "权限管理")
@RestController
@RequestMapping(value = "auth/perm")
public class PermissionController {

    @Autowired
    private AuthPermissionService permissionService;


    /**
     * 新增权限
     *
     * @return
     */
    @ApiOperation(value = "新增权限")
    @PostMapping("insert")
    public JsonResult insert(@RequestBody AuthPermission perm) {
        JsonResult jsonResult = new JsonResult<>();
        permissionService.insert(perm);
        return jsonResult;
    }

    /**
     * 编辑
     *
     * @return
     */
    @ApiOperation(value = "新增权限")
    @PostMapping("update")
    public JsonResult update(@RequestBody AuthPermission perm) {
        JsonResult jsonResult = new JsonResult<>();
        permissionService.update(perm);
        return jsonResult;
    }


    /**
     * 删除
     *
     * @return
     */
    @ApiOperation(value = "新增权限")
    @PostMapping("delete/{id}")
    public JsonResult delete(@PathVariable Long id) {
        JsonResult jsonResult = new JsonResult<>();
        if (permissionService.delete(id)) {
            return jsonResult;
        }
        jsonResult.setCode(AuthError.OPERATION_FAILED.code);
        jsonResult.setMsg(AuthError.OPERATION_FAILED.msg);
        return jsonResult;
    }

    /**
     * 分页查询所有
     *
     * @return
     */
    @ApiOperation(value = "分页查询所有")
    @GetMapping(value = "select_all_page")
    JsonResult selectAllPage(@RequestParam Integer pageNo, @RequestParam Integer pageSize) {
        JsonResult jsonResult = new JsonResult<>();
        Map params = new HashMap<>();
        Map resultData = new HashMap();
        params.put("", "");
        Page p = PageHelper.startPage(pageNo, pageSize);
        List<AuthPermission> perms = permissionService.selectList(params);
        resultData.put("list", perms);
        resultData.put("count", p.getTotal());
        jsonResult.setData(resultData);
        return jsonResult;
    }


    /**
     * 查询没有分配的权限
     *
     * @return
     */
    @ApiOperation(value = "查询没有分配的权限")
    @GetMapping(value = "select_no_use")
    JsonResult selectNoUse(Long mid) {
        JsonResult jsonResult = new JsonResult<>();
        Map params = new HashMap<>();
        params.put("mid", mid);
        jsonResult.setData(permissionService.selectNoUse(params));
        return jsonResult;
    }

    /**
     * 根据角色ID查询权限
     *
     * @param rid
     * @return
     */
    @ApiOperation(value = "根据角色ID查询权限")
    @GetMapping(value = "select_by_rid")
    JsonResult selectByRid(@RequestParam Long rid) {
        JsonResult jsonResult = new JsonResult<>();
        jsonResult.setData(permissionService.selectByRid(rid));
        return jsonResult;
    }

    /**
     * 根据菜单ID查询权限
     *
     * @param mid
     * @return
     */
    @ApiOperation(value = "根据菜单ID查询权限")
    @GetMapping(value = "select_list_by_mid")
    JsonResult selectByMid(@RequestParam Long mid) {
        JsonResult jsonResult = new JsonResult<>();
        jsonResult.setData(permissionService.selectByMid(mid));
        return jsonResult;
    }


    /**
     * 查询菜单所包含权限
     *
     * @return
     */
    @ApiOperation(value = "查询菜单所包含权限")
    @GetMapping("select_id_by_mid")
    public JsonResult<List<AuthMenu>> selectPerms(Integer mid) {
        JsonResult jsonResult = new JsonResult<>();
        jsonResult.setData(permissionService.selectPerms(mid));
        return jsonResult;
    }

    /**
     * 根据PID批量修改权限
     *
     * @return
     */
    @ApiOperation(value = "根据PID批量修改权限")
    @PostMapping("update_by_ids")
    public JsonResult updateByIds(@RequestBody Map requestBody) {
        JsonResult jsonResult = new JsonResult<>();
        List<Map> permsData = (List<Map>) requestBody.get("permsData");
        Map menuPermsData = (Map) requestBody.get("menuPermsData");

        Integer mid = (Integer) menuPermsData.get("mid");
        List<Integer> perms = (List<Integer>) menuPermsData.get("perms");

        List<AuthPermission> mapList = new ArrayList<>();
        for (Map map : permsData) {
            Integer id = (Integer) map.get("key");
            if (!perms.contains(id)) {
                AuthPermission perm = new AuthPermission();
                perm.setId(Long.valueOf(map.get("key") + ""));
                perm.setMid(-1);
                mapList.add(perm);
            }
        }
        jsonResult.setData(permissionService.updateByIds(mid, perms, mapList));
        return jsonResult;
    }

    /**
     * 根据RID批量修改角色权限
     *
     * @return
     */
    @ApiOperation(value = "根据RID批量修改角色权限")
    @PostMapping("update_by_rid")
    public JsonResult updateByRid(@RequestBody Map requestBody) {
        JsonResult jsonResult = new JsonResult<>();
        List<Integer> permsData = (List<Integer>) requestBody.get("permsData");
        Integer rid = Integer.valueOf(requestBody.get("rid") + "");
        List<Integer> pids = (List<Integer>) requestBody.get("perms");
        permissionService.updateByRids(permsData, rid, pids);
        return jsonResult;
    }
}
